﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CommonNet48.Extend
{
    /// <summary>
    /// 异常对象扩展类
    /// </summary>
    public static class ExceptionExtend
    {
        /// <summary>
        /// 获取出错文件名
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static string GetErrorFile(this Exception ex)
        {
            var sf = new System.Diagnostics.StackTrace(ex).GetFrame(0);
            return sf.GetFileName();
        }

        /// <summary>
        /// 获取出错方法名
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static string GetErrorMethodName(this Exception ex)
        {
            var sf = new System.Diagnostics.StackTrace(ex, true).GetFrame(0);
            return sf.GetMethod().Name;
        }

        /// <summary>
        /// 获取出错方法对象
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static MethodBase GetErrorMethod(this Exception ex)
        {
            var sf = new System.Diagnostics.StackTrace(ex, true).GetFrame(0);
            return sf.GetMethod();
        }

        /// <summary>
        /// 获取通用出错信息
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static string GetGenericErrorInfo(this Exception ex)
        {
            var sf = new System.Diagnostics.StackTrace(ex, true).GetFrame(0);
            return $"出错文件({sf.GetFileName()}第{sf.GetFileLineNumber()}行),出错方法({sf.GetMethod().Name})";
        }

    }
}
